package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.system.controller.dto.IndentOrderTask;
import cn.stylefeng.guns.modular.system.controller.dto.ZjAfterSaleOrderTask;
import cn.stylefeng.guns.modular.system.entity.DealerLevel;
import cn.stylefeng.guns.modular.system.entity.ZjAfterSaleOrderAudit;
import cn.stylefeng.guns.modular.system.model.params.IndentOrderCheckParam;
import cn.stylefeng.guns.modular.system.model.params.ZjAfterSaleOrderAuditParam;
import cn.stylefeng.guns.modular.system.service.AfterSaleService;
import cn.stylefeng.guns.modular.system.service.ZjAfterSaleOrderAuditService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 控制器
 *
 * @author 陶启然
 * @Date 2019-07-30 16:01:24
 */
@Controller
@RequestMapping("/zjAfterSaleOrderAudit")
public class ZjAfterSaleOrderAuditController extends BaseController {

    private String PREFIX = "/tao/zjAfterSaleOrderAudit";

    @Autowired
    private ZjAfterSaleOrderAuditService zjAfterSaleOrderAuditService;

    @Resource
    private TaskService taskService;
    @Autowired
    private AfterSaleService afterSaleService;
    /**
     * 跳转到主页面
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/zjAfterSaleOrderAudit.html";
    }

    /**
     * 新增页面
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zjAfterSaleOrderAudit_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/zjAfterSaleOrderAudit_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZjAfterSaleOrderAuditParam zjAfterSaleOrderAuditParam) {
        this.zjAfterSaleOrderAuditService.add(zjAfterSaleOrderAuditParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZjAfterSaleOrderAuditParam zjAfterSaleOrderAuditParam) {
        this.zjAfterSaleOrderAuditService.update(zjAfterSaleOrderAuditParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZjAfterSaleOrderAuditParam zjAfterSaleOrderAuditParam) {
        this.zjAfterSaleOrderAuditService.delete(zjAfterSaleOrderAuditParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZjAfterSaleOrderAuditParam zjAfterSaleOrderAuditParam) {
        ZjAfterSaleOrderAudit detail = this.zjAfterSaleOrderAuditService.getById(zjAfterSaleOrderAuditParam.getId());
        return ResponseData.success(detail);
    }

//    /**
//     * 查询列表
//     *
//     * @author 陶启然
//     * @Date 2019-07-30
//     */
//    @ResponseBody
//    @RequestMapping("/list")
//    public LayuiPageInfo list(ZjAfterSaleOrderAuditParam zjAfterSaleOrderAuditParam) {
//        return this.zjAfterSaleOrderAuditService.findPageBySpec(zjAfterSaleOrderAuditParam);
//    }


    @RequestMapping("/check")
    @ResponseBody
    public ResponseData check(ZjAfterSaleOrderAuditParam zjAfterSaleOrderAuditParam) {

        ZjAfterSaleOrderAudit zjAfterSaleOrderAudit = new ZjAfterSaleOrderAudit();
        BeanUtils.copyProperties(zjAfterSaleOrderAuditParam, zjAfterSaleOrderAudit);
        zjAfterSaleOrderAudit.setAuditTime(new Date());
        zjAfterSaleOrderAudit.setUserId(ShiroKit.getUserNotNull().getId());
        Task task = taskService.createTaskQuery().taskId(zjAfterSaleOrderAuditParam.getTaskId()).singleResult();
        zjAfterSaleOrderAudit.setProcessInstanceId(task.getProcessInstanceId());
        zjAfterSaleOrderAuditService.save(zjAfterSaleOrderAudit);

        //启动审核流程
        Map<String, Object> map = new HashMap<>();
        map.put("status", zjAfterSaleOrderAuditParam.getAuditStatus());
        taskService.complete(task.getId(), map);

        return ResponseData.success(200, "审核成功", zjAfterSaleOrderAudit);
    }

    @ResponseBody
    @RequestMapping("/list")
    @Permission({"订货单审核人员","主管"})
    public LayuiPageInfo list() {
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey("pc_indent_order").taskCandidateUser(ShiroKit.getUserNotNull().getId() + "").list();
        if(tasks.size() == 0) {
            return new LayuiPageInfo();
        }


        //根据流程实例ID查询请假申请表单数据
        List<ZjAfterSaleOrderTask> zjAfterSaleOrderTasks = tasks.stream().filter(task -> afterSaleService.getByProcessInstanceId(task.getProcessInstanceId())!=null).map(task -> {
            ZjAfterSaleOrderTask zjAfterSaleOrderTask = new ZjAfterSaleOrderTask();
            BeanUtils.copyProperties(afterSaleService.getByProcessInstanceId(task.getProcessInstanceId()), zjAfterSaleOrderTask);
            zjAfterSaleOrderTask.setProcessInstanceId(task.getProcessInstanceId());
            zjAfterSaleOrderTask.setTaskId(task.getId());
            return zjAfterSaleOrderTask;
        }).collect(Collectors.toList());
        LayuiPageInfo result = new LayuiPageInfo();
        result.setCount(zjAfterSaleOrderTasks.size());
        result.setData(zjAfterSaleOrderTasks);
        return result;

//        /*Long addressId = addressRoleService.getOne(new QueryWrapper<AddressRole>()
//                .eq("user_id", ShiroKit.getUserNotNull().getId())).getAddressId();*/
//        Integer type = null;
//        if (ShiroKit.hasRole("订货单审核人员")) {
//            type = StatusCode.PENDING.getVal();
//        }else if (ShiroKit.hasRole("主管")){
//            type= StatusCode.INDENTSUCCESS.getVal();
//        }
//        return this.indentOrderCheckService.findPageBySpec(indentOrderCheckParam, type);
    }
}


